package com.xcf.controller.business.finance;

import cn.com.xcf.dao.model.TFinFeeCheckExample;
import cn.com.xcf.service.FeeCheckService;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import com.xcf.controller.base.BaseController;
import com.xcf.entity.Page;
import com.xcf.service.business.finance.AccountCheckService;
import com.xcf.util.Jurisdiction;
import com.xcf.util.PageData;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import java.util.HashMap;
import java.util.List;
import java.util.Map;


@Controller
@RequestMapping(value="/accountCheck")
public class AccountCheckController extends BaseController {
	
	String menuUrl = "/accountCheck/index"; //菜单地址(权限用)
    final String base = "/business/finance";
    private static final String PREFIX = "/business/finance/";

	@Autowired
	private FeeCheckService feeCheckService;
	
	@Autowired
	private AccountCheckService accountCheckService;

	/**列表
	 * @param page
	 * @throws Exception
	 */
	@RequestMapping(value="/index")
	public ModelAndView list(Page page, HttpServletRequest req) throws Exception{
	
        ModelAndView mv = getModelAndView();
        try{
        	mv.addObject("pd", new HashMap());
            mv.setViewName(PREFIX.concat("accountCheck/account_check"));
        } catch(Exception ex){
            //log.error("query-check-error:"+param,ex);
        }
        return mv;
	}
	
	/**列表
	 * @param page
	 * @throws Exception
	 */
	@RequestMapping(value="/listAccount")
	public ModelAndView account(Page page, HttpServletRequest req) throws Exception{
		Map param = getParams(req);//new PageData(req);
        TFinFeeCheckExample e = new TFinFeeCheckExample();
		e.createCriteria();
		e.setPage(page);
		handlePageParams(param,e.getPage());
        ModelAndView mv = getModelAndView();
        try{
            //List<TFinFeeCheck> ret = feeCheckService.selectByPage(e,param,page);
            PageData pd = getPageData();
            
            pd=this.getPageData();
            if(pd.isEmpty()){
            	pd.put("bill_examine", null);
            }
            page.setPd(pd);
            //List<TFinFeeCheck>ret  = accountCheckService.selectByPage(e,param,page);
            //accountCheckService.
        	List<PageData>ret = accountCheckService.findAccountCheck(e,param,page);
            mv.addObject("dataList",ret);
            mv.addObject("pd", page);
            mv.addObject("QX",Jurisdiction.getHC());//按钮权限
            mv.setViewName(PREFIX.concat("panel/account_check_top"));
        } catch(Exception ex){
            log.error("query-check-error:"+param,ex);
        }
        return mv;
	}

	
	
	
	
	
	
	

	@RequestMapping(value="/incomeList",method = RequestMethod.POST)
	public ModelAndView incomeList(Page page,
			HttpServletRequest req,HttpServletResponse res) throws Exception{
		//CommonResponse response = new CommonResponse();
//		String operator = getLoginUser(req,res).getUSER_ID();
		/*PageData pd = new PageData();
		pd = this.getPageData();
        ModelAndView view = getModelAndView();*/
        Map param = getParams(req);//new PageData(req);
        TFinFeeCheckExample e = new TFinFeeCheckExample();
		e.createCriteria();
		e.setPage(page);
		handlePageParams(param,e.getPage());
        ModelAndView view = getModelAndView();
       
            PageData pd = getPageData();
            pd=this.getPageData();
            if(pd.isEmpty()){
            	pd.put("bill_examine", null);
            }
            page.setPd(pd);
		try {
//			assert checkId!=null && checkId.length()>0 /*&& undoList!=null && undoList.length()>0*/;
//			Object data = checkService.listItemsByCheckId(checkId);
//			view.addObject("data",data);
			List<PageData>ret = accountCheckService.findAccountDetil(e,param,page);
			view.addObject("data",ret);
            view.addObject("QX", Jurisdiction.getHC());//
			view.setViewName(base.concat("/panel/account_check_income"));
		}catch (Exception ex){

		}
		return view;
	}
	@RequestMapping(value="/costList",method = RequestMethod.POST)
	public ModelAndView costList(Page page,
			HttpServletRequest req,HttpServletResponse res) throws Exception{
		PageData pd = new PageData();
		pd = this.getPageData();
        ModelAndView view = getModelAndView();
        
        
        
        Map param = getParams(req);//new PageData(req);
        TFinFeeCheckExample e = new TFinFeeCheckExample();
		e.createCriteria();
		e.setPage(page);
		handlePageParams(param,e.getPage());
        //ModelAndView view = getModelAndView();
       
            //List<TFinFeeCheck> ret = feeCheckService.selectByPage(e,param,page);
            //PageData pd = getPageData();
            
            //pd=this.getPageData();
            if(pd.isEmpty()){
            	pd.put("bill_examine", null);
            }
            page.setPd(pd);
        
        
		try {
			List<PageData>ret = accountCheckService.findCostDetil(e,param,page);
			view.addObject("info",ret);
            view.addObject("QX", Jurisdiction.getHC());//
			view.setViewName(base.concat("/panel/account_check_cost"));
		}catch (Exception ex){
		}
		return view;
	}
	/**
	 *更新状态
	 * @param
	 * @throws Exception
	 */
	@RequestMapping(value="/save")
	@ResponseBody
	public String save() throws Exception{
		PageData pd = new PageData();
		pd = this.getPageData();
		String data = pd.getString("dataStr");

		JSONArray items = JSONArray.fromObject(data);
		for(int i=0,size=items.size();i<size;i++){
			JSONObject item = items.getJSONObject(i);
			if(!item.getString("type").isEmpty()){
				PageData pdq = new PageData();
				if(item.getString("bill_examine").equals("1")){
					pdq.put("id", Integer.parseInt(item.getString("id")));
					pdq.put("bill_examine", 1);
				}else if(item.getString("bill_examine").equals("0")){
					pdq.put("id", Integer.parseInt(item.getString("id")));
					pdq.put("bill_examine", 0);
				}
				if(item.getString("type").equals("1")){
					
					accountCheckService.updateIncomeState(pdq);
				}else if(item.getString("type").equals("0")){
					accountCheckService.updateCostState(pdq);
				}
			}
		}

		return null;
	}

	/**删除
	 * @throws Exception
	 */
	@RequestMapping(value="/delete")
	@ResponseBody
	public String delete() throws Exception{
		PageData pd = new PageData();
		pd = this.getPageData();
		String data = pd.getString("dataStr");

		JSONArray items = JSONArray.fromObject(data);
		for(int i=0,size=items.size();i<size;i++){
			JSONObject item = items.getJSONObject(i);
			if(!item.getString("type").isEmpty()){
				PageData pdq = new PageData();
				if(item.getString("bill_examine").equals("1")){
					pdq.put("id", Integer.parseInt(item.getString("id")));
					pdq.put("bill_examine", 1);
				}else if(item.getString("bill_examine").equals("0")){
					pdq.put("id", Integer.parseInt(item.getString("id")));
					pdq.put("bill_examine", 0);
				}
				if(item.getString("type").equals("1")){
					
					accountCheckService.updateIncomeState(pdq);
				}else if(item.getString("type").equals("0")){
					accountCheckService.updateCostState(pdq);
				}
			}
		}
		return null;

	}

	/**修改
	 * @param
	 * @throws Exception
	 */
	@RequestMapping(value="/edit")
	public ModelAndView edit() throws Exception{
       
        return null;
	}

	/**去新增页面
	 * @param
	 * @throws Exception
	 */
	@RequestMapping(value="/goAdd")
	public ModelAndView goAdd()throws Exception{
		ModelAndView mv = new ModelAndView();
		/*
		mv.setViewName(PREFIX.concat("/tFinFeeCheck_edit"));
		mv.addObject("msg", "save");
		mv.addObject("pd", pd);*/
		return mv;
	}

	/**去修改页面
	 * @param
	 * @throws Exception
	 */
	@RequestMapping(value="/goEdit")
	public ModelAndView goEdit()throws Exception{
		ModelAndView mv = this.getModelAndView();
		/*pd = tFinFeeCheckService.findById(pd);	//根据ID读取
		mv.setViewName(PREFIX.concat("/tFinFeeCheck_edit"));
		mv.addObject("msg", "edit");
		mv.addObject("pd", pd);*/
		return mv;
	}

	/**
	 * 查看详情
	 * @param
	 * @throws Exception
	 */
	@RequestMapping(value="/detail")
	@ResponseBody
	public Object detail(Object id) throws Exception{
		return null;
	}

}
